// Copyright (C) 2024 Kumo inc.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
//

#include <kllm/kai/embeddings.h>
#include <kllm/kai/proto_helper.h>

namespace kllm {

    void KaiEmbeddings::embedding(const KaiRequest &req, KaiResponse &response) {
        if (req.query_type() != QUERY_EMBEDDING) {
            response.mutable_status()->set_code(static_cast<int32_t>(turbo::StatusCode::kInvalidArgument));
            response.mutable_status()->set_errmsg("invalid query type for list model, it should be as QUERY_EMBEDDING");
            return;
        }
        response.set_query_type(QUERY_EMBEDDING);
        response.set_model_alias(_context->params.model_alias);
        turbo::Time start = turbo::Time::current_time();
        // create and queue the task
        {
            std::vector<server_task> tasks = _context->create_tasks_inference(req, SERVER_TASK_INF_TYPE_EMBEDDING, req.embedding_request().prompts());
            _context->queue_results.add_waiting_tasks(tasks);
            _context->queue_tasks.post(tasks);

            // get the result
            std::unordered_set<int> task_ids = server_task::get_list_id(tasks);

            _context->receive_cmpl_results(task_ids, [&](std::vector<TaskResult> &results) {
                LOG(INFO) << "task size: " << results.size();
                for (const auto &res: results) {
                    LOG(INFO) << res.embeddings.embedding().size();
                    for (auto &it: res.embeddings.embedding()) {
                        *response.mutable_embedding_response()->mutable_embedding()->Add() = it;
                    }
                }
            }, [&response](const turbo::Status &status) {
                response.mutable_status()->set_code(status.raw_code());
                response.mutable_status()->set_errmsg(std::string(status.message()));
            });

            _context->queue_results.remove_waiting_task_ids(task_ids);
        }
        turbo::Time end = turbo::Time::current_time();
        response.set_elapsed(turbo::Duration::to_double_seconds(end - start));
    }

}  // namespace kllm
